// pages/Tabbar/Home/Home.js
//获取应用实例
const app = getApp()
const assetsAddress = app.globalData.global.assetsIp + '/' + app.globalData.global.assetsName;
import { queryAppInforMation } from '../../../api/brand';
import { ACCESS_TOKEN } from '../../../utils/types';
const regeneratorRuntime = require('../../../common/runtime');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    assetsAddress: assetsAddress,
    brandId: app.globalData.global.brandId,
    bannerList: [],
    message: '重要通知：平台上线了！！！',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getBannerList();
    this.getMes();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getBannerList();
    this.getMes();
    setTimeout(()=>{
      wx.stopPullDownRefresh({
        success: (res) => {
          console.log(res,'刷新成功')
          wx.showToast({
            title: '刷新成功',
            icon: 'none',
          })
        },
      })
    },1000)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let userId = wx.getStorageSync('userId')
    return {
      title: this.data.shareTitle,
      path: '/pages/Welcome/Welcome?scene='+userId,
    }
  },
  // 获取轮播图信息
  getBannerList() {
    let _this = this
    queryAppInforMation({infoType: 1})
    .then(res => {
      if (res.success) {
        _this.setData({
          bannerList: res.data.content
        })
      } else {
        wx.showToast({
          title: res.errMessage,
          icon: 'none'
        })
      }
    })
  },
  // 获取公告消息
  getMes(){
    let _this = this;
    queryAppInforMation({infoType: '5'})
    .then(res => {
      if(res.success){
        if(res.data.content.length > 0){
          _this.setData({
            message: res.data.content[0].content,
          })
        }
      }else{
        wx.showToast({
          title: res.errMessage,
          icon: 'none',
        })
      }
    })
  },
  // 页面跳转
  goNext(e){
    let type = e.target.dataset.type||e.currentTarget.dataset.type;
    let url = e.target.dataset.name||e.currentTarget.dataset.name;
    let item = e.target.dataset.item||e.currentTarget.dataset.item;
    // return
    if(item){
      if(type == 1){
        wx.switchTab({
          url: `${url}`,
        })
      }else{
        wx.navigateTo({
          url: `${url}?item=${JSON.stringify(item)}`,
        })
      }
    }else{
      if(type == 1){
        wx.switchTab({
          url: `${url}`,
        })
      }else{
        wx.navigateTo({
          url: `${url}`,
        })
      }
    }
  },
  // 跳转链接
  goLink(e){
    let _this = this;
    let item = e.target.dataset.item||e.currentTarget.dataset.item;
    queryAppInforMation({infoType: '2',title: item,page: 0})
    .then(res => {
      if(res.success){
        if(res.data.content.length > 0){
          var itm = res.data.content[0];
          if(res.data.content[0].imgUrl){
            wx.navigateTo({
              url: `/pages/Link/LinkImg/LinkImg?url=${encodeURIComponent(itm.imgUrl)}&title=${JSON.stringify(item)}`,
            })
          }else if(itm.jumpUrl && itm.jumpUrl.length > 4 && itm.jumpUrl.substring(0,4) == 'http'){
            if(itm.isAddParam == '1'){
              var phone = wx.getStorageSync('phone');
              var userId = wx.getStorageSync('userId');
              var token = wx.getStorageSync(ACCESS_TOKEN);
              var jumpUrl = `${itm.jumpUrl}?phone=${phone}&token=${token}&brandId=${this.data.brandId}&userId=${userId}`;
              console.log(jumpUrl,'jumpUrl')
              // if(itm.title == '评分快查'){
              //   wx.navigateTo({
              //     url: `/pages/Systems/Shares/Shares?url=${encodeURIComponent(jumpUrl)}`,
              //   })
              // }else{
              //   wx.navigateTo({
              //     url: `/pages/Link/Link/Link?url=${encodeURIComponent(jumpUrl)}&title=${JSON.stringify(item)}`,
              //   })
              // }
              wx.navigateTo({
                url: `/pages/Link/Link/Link?url=${encodeURIComponent(jumpUrl)}&title=${JSON.stringify(item)}`,
              })
            }else{
              wx.navigateTo({
                url: `/pages/Link/Link/Link?url=${encodeURIComponent(itm.jumpUrl)}&title=${JSON.stringify(item)}`,
              })
            }
          }else if(res.data.content[0].content){
            wx.navigateTo({
              url: `/pages/Link/LinkText/LinkText?content=${JSON.stringify(itm.content)}&title=${JSON.stringify(item)}`,
            })
          }
        }else{
          wx.showToast({
            title: '敬请期待',
            icon: 'none',
          })
        }
      }else{
        wx.showToast({
          title: res.errMessage,
          icon: 'none',
        })
      }
    })
  },
})